<?php
/**
 * class 
 * 
 * @package iforum.common
 * @author iCer<icer@justdn.com>
 * $Id: TplComplier.php,v 1.3 2005/04/06 04:07:55 icer Exp $
**/
class TplComplier
{
	function complier($tpl)
	{
		//$tplpath = ROOT_PATH.'/tpl';
		$ctplpath = ROOT_PATH.'/tpl_c';
		
		$ctplpath = dirname(str_replace('/tpl/', '/tpl_c/', $tpl));
		
		$pa = "/(.*)\/tpl\/([^\/]*?)\/(.*)\.php/i"; 
		preg_match_all($pa, $tpl, $cinfo, PREG_SET_ORDER);


		
		$tplname = basename($tpl);
		$action = $cinfo[0][3];
		$style =  $cinfo[0][2];

		if (!is_dir($ctplpath))
		{
			mkdir($ctplpath,777);
		}		

		$tpltime = filemtime($tpl);

		$resourcename = $ctplpath."/".$action."_r".'.php';
		$templetename = $ctplpath."/".$action."_t".'.xsl';


		$str = file_get_contents($tpl);
		$result = array();
		$include = '';

		$pattern = "/\{tag=([^\{\}]*?)\}/i";
		preg_match_all($pattern, $str, $tags, PREG_SET_ORDER);
		if (count($tags)>=1)
		{
			foreach($tags as $tag)
			{
				$pa = "/(.*?)\?(.*?)/i";
				if (preg_match($pa, $tag[1])>0) //have params
				{
					$a = explode('?',$tag[1]);
					$tmp['service'] = $a[0];
					
					$params = explode('&', $a[1]);

					foreach($params as $param)
					{
						$arr = explode('=', $param);
						$tmp['param'][$arr[0]] = $arr[1];
					}					
					
				}
				else //no params
				{
					$tmp['service'] = $tag[1];
					$tmp['param'] = '';
				}
				
				$result[] = $tmp;
				if ($tmp['param']['tplname'] !='' || isset($tmp['param']['tplname']))
				{
					$include.= "<xsl:include href=\"../../tpl/".$style."/".$tmp['param']['tplname'].".xsl"."\"/>\n";	
				}			
				else 
				{
					$include.= "<xsl:include href=\"../../tpl/".$style."/".$tmp['service'].".xsl"."\"/>\n";	
				}
				
				if (isset($tmp['param']['name']) || $tmp['param']['name'] != '')
				{
					if ($tmp['param']['tplname'] !='' || isset($tmp['param']['tplname']))
					{
						$replace = "<xsl:apply-templates select=\"".$tmp['param']['name']."\" mode=\"".$tmp['param']['tplname']."\"/>";
					}
					else 
					{
						$replace = "<xsl:apply-templates select=\"".$tmp['param']['name']."\" mode=\"".$tmp['service']."\"/>";
					}
				}
				elseif ($tmp['param']['httpinvoke']=='true')
				{
					$replace = "<xsl:apply-templates select=\"document('../../httpinvoke.php?service=".$tmp['service']."')\" mode=\"".$tmp['service']."\"/>";
				}
				else 
				{
					$replace = "<xsl:apply-templates select=\"".$tmp['service']."\" mode=\"".$tmp['service']."\"/>";
				}
				
				$str = str_replace($tag[0], $replace, $str);
			}
		}		
		
		//rediect & error tpl 
		$a = "<xsl:template match=\"//redirect\">
			  <xsl:apply-templates select=\".\" mode=\"redirect\"/>
			  </xsl:template>";
		$a.= "<xsl:template match=\"//error\">
			  <xsl:apply-templates select=\".\" mode=\"error\"/>
			  </xsl:template>";
		$b = "<xsl:include href=\"../../tpl/".$style."/redirect.xsl"."\"/>\n";
		$b.= "<xsl:include href=\"../../tpl/".$style."/error.xsl"."\"/>\n";
		
		for ($i=0; $i<count($result); $i++)
		{
			if($result[$i]['param']['httpinvoke'] == 'true')
			{
				unset($result[$i]);
			}
		}
		$result = serialize($result);
		
		$fp = fopen($resourcename, 'wb');
		fwrite($fp, $result);
		fclose($fp);
		
		
		$str = "<?xml version=\"1.0\" encoding=\"GB2312\"?>"
			   ."
			   <!DOCTYPE xsl:stylesheet [
			   <!ENTITY copy \"&#169;\">
			   <!ENTITY nbsp \"&#160;\">
			   ]>
			   <xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
			   <xsl:output method=\"html\"/>
			   <xsl:param name=\"stylefolder\">tpl/".$style."</xsl:param>
			   <xsl:template match=\"//result\">"
			   .$str."</xsl:template>".$a.$include.$b."</xsl:stylesheet>";
		$fp = fopen($templetename, 'wb');
		fwrite($fp, $str);
		fclose($fp);
		
		touch($resourcename, $tpltime);
		touch($templetename, $tpltime);
	}
	

}
?>